package com.example.k.littletag.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.k.littletag.R;
import com.example.k.littletag.adapter.FeedAdapter;
import com.example.k.littletag.db.NoteDB;
import com.example.k.littletag.utils.Note;

import java.util.List;

public class MainActivity extends ActionBarActivity implements FeedAdapter.OnRecyclerViewItemClickListener {
    public static final String CHECK_COUNT = "check_count";
    public static final String CONTENT_SAVE = "content_save";
    public static final String TIME_PICKED = "time_picked";
    public static final String ARG_DRAWING_START_LOCATION = "drawing_start_location";
    public static final String DELETE_ITEM = "delete_item";

    private ImageView btnCreate;
    private TextView newMenoText;
    private RecyclerView rvFeed;
    private FeedAdapter feedAdapter;
    private List<Note> noteList;
    private NoteDB noteDB;


    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            feedAdapter.notifyDataSetChanged();
            handler.postDelayed(this, 60 * 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        setupBtnCreate();

        handler.postDelayed(runnable, 1000 * 60);
        setupFeed();

    }


    @Override
    protected void onResume() {
        feedAdapter.notifyDataSetChanged();
        rvFeed.setAdapter(feedAdapter);
        super.onResume();
    }

    private void initView() {
        btnCreate = (ImageView) findViewById(R.id.btnCreate);
        rvFeed = (RecyclerView) findViewById(R.id.recycler_view);
        noteDB = NoteDB.getNoteDB(this);
        newMenoText = (TextView) findViewById(R.id.newMenoText);
        noteList = noteDB.loadNote();
        if (noteList.size() >= 2) {
            newMenoText.setVisibility(View.INVISIBLE);
        }

    }

    private void setupBtnCreate() {
        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                location[0] += v.getMeasuredWidth();
                location[1] += v.getMeasuredHeight();
                v.getLocationInWindow(location);
                SaveNote.start(location, MainActivity.this);
            }
        });
    }


    private void setupFeed() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvFeed.setLayoutManager(linearLayoutManager);
        rvFeed.setItemAnimator(new DefaultItemAnimator());
        feedAdapter = new FeedAdapter(this, noteList);
        feedAdapter.setOnRecyclerViewItemClickListener(this);
        rvFeed.setAdapter(feedAdapter);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        int position;
        switch (resultCode) {
            case 1:
                position = data.getIntExtra(DELETE_ITEM, -1);
                noteDB.deleteNote(noteList.get(position));
                feedAdapter.removeItem(noteList.get(position));

                //rvFeed.setAdapter(feedAdapter);
                break;

            case 2:
                /**
                 * 又成了最烂的代码
                 */
                position = data.getIntExtra(DELETE_ITEM, -1);
                Note note = noteList.get(position);
                note.setIsFinish(1);
                noteDB.deleteNote(note);
                noteDB.saveNote(note);
                noteList = noteDB.loadNote();
                feedAdapter = new FeedAdapter(this, noteList);
                feedAdapter.setOnRecyclerViewItemClickListener(this);
                rvFeed.setAdapter(feedAdapter);
                break;

            case RESULT_CANCELED:
                break;
        }

    }

    @Override
    public void onItemClick(View v, int position) {
        int[] location = new int[2];
        v.getLocationInWindow(location);

        location[1] += v.getMeasuredHeight() / 2;

        Intent intent = new Intent(getApplication(), PreActivity.class);
        intent.putExtra(ARG_DRAWING_START_LOCATION, location[1]);
        intent.putExtra(DELETE_ITEM, position);

        intent.putExtra(TIME_PICKED, noteList.get(position).getTime());
        intent.putExtra(CONTENT_SAVE, noteList.get(position).getContent());
        intent.putExtra(CHECK_COUNT, noteList.get(position).getCheckCount());
        intent.putExtra("isFinish", noteList.get(position).getIsFinish());
        startActivityForResult(intent, 1);
    }

}
